package com.flower8.service.impl;

import com.flower8.service.SysLogService;
import com.flower8.sysconfig.constants.UserConstants;
import com.flower8.dao.SysAdminDao;
import com.flower8.model.SysAdmin;
import com.flower8.service.SysAdminService;
import com.flower8.utils.PageData;
import com.flower8.utils.SpringUtil;
import com.flower8.utils.UserUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @Description: 管理员service实现类
 * @Author: tanglin
 * @Date: 2019/09/04 17:16
 * @Version: 1.0
 */
@Service
public class SysAdminServiceImpl implements SysAdminService {
    private static final Logger log = LoggerFactory.getLogger("adminLogger");
    @Autowired
    private SysAdminDao sysAdminDao;

    /**
     * 根据条件查询系统管理员
     * @param pd
     * @return
     */
    @Override
    public List<SysAdmin> getAllAdmin(PageData pd) throws Exception {
        return sysAdminDao.getAllAdmin(pd);
    }

    /**
     * 根据条件查询系统管理员数量
     *
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getAllAdminCount(PageData pd) throws Exception {
        return sysAdminDao.getAllAdminCount(pd);
    }

    /**
     * 更新管理员信息
     *
     * @param sysAdmin
     * @throws Exception
     */
    @Override
    @Transactional
    public void update(SysAdmin sysAdmin) throws Exception {
        if(sysAdmin.getPassword() != null && !"".equals(sysAdmin.getPassword())){
            //获取修账号信息
            SysAdmin adm = sysAdminDao.getById(sysAdmin.getId());
            sysAdmin.setPassword(passwordEncoder(sysAdmin.getPassword(),adm.getSalt()));
        }
        sysAdminDao.update(sysAdmin);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "更新管理员信息", UserUtil.getCurrentUser().getAccount()+"更新管理员信息");
    }

    /**
     * 删除管理员
     *
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void delete(Long id) throws Exception {
        sysAdminDao.delete(id);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "删除id为"+id+"的管理员", UserUtil.getCurrentUser().getAccount()+"删除管理员");
    }

    /**
     * 新增管理员
     *
     * @param sysAdmin
     * @return
     */
    @Override
    @Transactional
    public void save(SysAdmin sysAdmin) {
        sysAdmin.setSalt(DigestUtils
                .md5Hex(UUID.randomUUID().toString() + System.currentTimeMillis() + UUID.randomUUID().toString()));
        sysAdmin.setPassword(passwordEncoder(sysAdmin.getPassword(), sysAdmin.getSalt()));
        log.debug("新增用户{}", sysAdmin.getAccount());
        sysAdminDao.save(sysAdmin);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "新增管理员", UserUtil.getCurrentUser().getAccount()+"新增管理员："+sysAdmin.getAccount());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public SysAdmin getById(Long id) {
        return sysAdminDao.getById(id);
    }

    /**
     * 根据管理员账号查询
     *
     * @param account
     * @return
     */
    @Override
    public SysAdmin getUser(String account) {
        return sysAdminDao.getUser(account);
    }

    /**
     * 加密
     * @param credentials
     * @param salt
     * @return
     */
    @Override
    public String passwordEncoder(String credentials, String salt) {
        Object object = new SimpleHash("MD5", credentials, salt, UserConstants.HASH_ITERATIONS);
        return object.toString();
    }

    /**
     * 修改账号密码
     * @param id          账号id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @throws Exception
     */
    @Override
    public String changePassword(Long id, String oldPassword, String newPassword) throws Exception {
        String msg = "";
        SysAdmin adm = sysAdminDao.getById(id);
        if (!adm.getPassword().equals(passwordEncoder(oldPassword, adm.getSalt()))) {
            msg = "原密码错误";
        }
        SysAdmin admin = new SysAdmin();
        admin.setId(id);
        admin.setPassword(passwordEncoder(newPassword, adm.getSalt()));
        admin.setStatus(-1);
        sysAdminDao.update(admin);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "修改管理员密码", UserUtil.getCurrentUser().getAccount()+"修改管理员密码");
        log.debug("修改{}的密码", adm.getAccount());
        return msg;
    }
}
